/***************************************************************************
创建者：华磊
 开始时间：         : 2020.11.09
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有修改人，修改时间，修改描述）

 ***************************************************************************
 *   程序寸动视窗
*                                                                         *
 ***************************************************************************/
#include "jogview.h"
#include "ui_jogview.h"
#include "hmicommondefine.h"
#include "instructionEnum.h"
#include <QMessageBox>
#include <QSettings>

//对各成员变量进行初始化
JogView::JogView(int styleId, int coRobotFlag, GlobalData *globalDataIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::JogView)
{
    globalData=globalDataIn;
    ui->setupUi(this);

    setGeometry(75,130,460,595);
    initialDisplayStyle(styleId);

    if(0==coRobotFlag)
    {
        ui->checkBox_dragMode->hide();
    }

    jValueLable.append(ui->label_Value1);
    jValueLable.append(ui->label_Value2);
    jValueLable.append(ui->label_Value3);
    jValueLable.append(ui->label_Value4);
    jValueLable.append(ui->label_Value5);
    jValueLable.append(ui->label_Value6);
//    jValueLable.append(ui->label_Value7);

    jUnitLable.append(ui->label_Unit1);
    jUnitLable.append(ui->label_Unit2);
    jUnitLable.append(ui->label_Unit3);
    jUnitLable.append(ui->label_Unit4);
    jUnitLable.append(ui->label_Unit5);
    jUnitLable.append(ui->label_Unit6);
//    jUnitLable.append(ui->label_Unit7);

    jProgressValueBar.append(ui->progressBar_1);
    jProgressValueBar.append(ui->progressBar_2);
    jProgressValueBar.append(ui->progressBar_3);
    jProgressValueBar.append(ui->progressBar_4);
    jProgressValueBar.append(ui->progressBar_5);
    jProgressValueBar.append(ui->progressBar_6);
//    jProgressValueBar.append(ui->progressBar_7);

    jShowWidget.append(ui->widgetmanuaul1);
    jShowWidget.append(ui->widgetmanuaul2);
    jShowWidget.append(ui->widgetmanuaul3);
    jShowWidget.append(ui->widgetmanuaul4);
    jShowWidget.append(ui->widgetmanuaul5);
    jShowWidget.append(ui->widgetmanuaul6);
//    jShowWidget.append(ui->widgetmanuaul7);

    //****************************

//    initFlag = true;

    //****************************
    totalAxisCount = 0;

//    robotAxisCount = 0;

    jValue.resize(D_ROBOT_AXIS_MAX);
    jUnitStr.resize(D_ROBOT_AXIS_MAX);


    QSettings settings(D_SHOW_STYLE_PATH, QSettings::IniFormat);
    int isShowClearMsgServon=  settings.value("Common/isShowClearMsgServon").toInt();
    if(0==isShowClearMsgServon)
    {
        ui->button_clrMsgAndServoOn->hide();
    }

}

JogView::~JogView()
{

}
void JogView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->widget->setGeometry(0,0,600,450);
        ui->widgetmanuaul1->setGeometry(105,15+2,430,45);
        ui->widgetmanuaul2->setGeometry(105,75+10+4,430,45);
        ui->widgetmanuaul3->setGeometry(105,135+20+5,430,45);
        ui->widgetmanuaul4->setGeometry(105,195+30+5,430,45);
        ui->widgetmanuaul5->setGeometry(105,255+40+6,430,45);
        ui->widgetmanuaul6->setGeometry(105,315+50+7,430,45);

        ui->button_clrMsgAndServoOn->setGeometry(0,5,110,40);
        ui->checkBox_sevon->setGeometry(0,50,90,40);
        ui->checkBox_dragMode->setGeometry(0,100,90,40);
        //ui->checkBox_joyStick->setGeometry(85+140+30+120,390,90,40);
        break;
    default:
        break;
    }
}


void JogView::changePageAxisStyleUpdate(bool isJointShow, int totalAxisCountIn, int pageIdIn,int onePageMaxAxisCount,
                                        std::vector<int> axisUnitIn, std::vector<std::vector <float> >  axisLimitIn)
{
    //显示的轴数
    totalAxisCount = totalAxisCountIn;

    //功能限制
    jogUseLimit(isJointShow,totalAxisCountIn);

    //软限位
    axisLimit.clear();
    if(isJointShow)
    {
        for(int i = pageIdIn*onePageMaxAxisCount;i<pageIdIn*onePageMaxAxisCount+onePageMaxAxisCount;i++)
        {
            QVector<float> tmp;
            if(i<totalAxisCountIn && i<axisLimitIn.size())
            {
                if(2 == axisLimitIn[i].size())
                {
                    tmp.append(axisLimitIn[i][0]);
                    tmp.append(axisLimitIn[i][1]);
                    axisLimit.append(tmp);
                }
                else
                {
                    tmp.append(-100);
                    tmp.append(100);
                    axisLimit.append(tmp);
                }
            }
        }
        //效果不好，超出１００后就不是比例变化了
//        for(int i=0;i<axisLimit.size();i++)
//        {
//            jProgressValueBar[i]->setRange(axisLimit[i][0],axisLimit[i][1]);
//            qDebug()<<"joint prograss i"<<i<<jProgressValueBar[i]->minimum()<<jProgressValueBar[i]->maximum();
//        }
    }
    else
    {
        //效果不好，超出１００后就不是比例变化了
//        for(int i=0;i<jProgressValueBar.size();i++)
//        {
//            jProgressValueBar[i]->setRange(-1000.0,1000.0);
//            qDebug()<<"coordinate prograss i"<<i<<jProgressValueBar[i]->minimum()<<jProgressValueBar[i]->maximum();
//        }

        for(int i=0;i<6;i++)
        {
            QVector<float> tmp;
            tmp.append(-1000.0);
            tmp.append(1000.0);
            axisLimit.append(tmp);
        }
    }


    //轴单位
    axisUnit.clear();
    if(isJointShow)
    {
        for(int i = pageIdIn*onePageMaxAxisCount;i<pageIdIn*onePageMaxAxisCount+onePageMaxAxisCount;i++)
        {
            if(i<totalAxisCountIn && i<axisUnitIn.size())
            {
                axisUnit.append(axisUnitIn[i]);
            }
        }

    }
    else
    {
        axisUnit.append(1);
        axisUnit.append(1);
        axisUnit.append(1);
        axisUnit.append(0);
        axisUnit.append(0);
        axisUnit.append(0);
    }
    for(int i=0;i<axisUnit.size();i++)
    {
        if(axisUnit[i] == EM_AXIS_DEG )
        {
            jUnitLable[i]->setText(tr("deg"));
        }
        else
        {
            jUnitLable[i]->setText(tr("mm"));
        }
    }

    for(int i =0;i<jShowWidget.size();i++)
    {
        jShowWidget[i]->hide();
    }

    for(int i =0;i<axisUnit.size();i++)
    {
        jShowWidget[i]->show();
    }

    for(int i=0;i<jProgressValueBar.size();i++)
    {
        jProgressValueBar[i]->setRange(0,100);
    }

    return ;
}

void JogView::updateAxisPositionShow(bool isJointShow, int totalAxisCountIn, int pageIdIn, int onePageMaxAxisCount,
                                     std::vector<double> positionValueIn)
{

    jValue.clear();
    if(isJointShow)
    {
        for(int i = pageIdIn*onePageMaxAxisCount;i<pageIdIn*onePageMaxAxisCount+onePageMaxAxisCount;i++)
        {
            if(i<totalAxisCountIn && i<positionValueIn.size())
            {
                jValue.append(positionValueIn[i]);
            }
        }
    }
    else
    {
        for(int i = pageIdIn*onePageMaxAxisCount;i<pageIdIn*onePageMaxAxisCount+onePageMaxAxisCount;i++)
        {
            if(i<positionValueIn.size())
            {
                jValue.append(positionValueIn[i]);
            }
        }
    }

    //更新 值到相应控件上
    for(int i = 0;i<jValue.size() ;i++)
    {
        jValueLable[i]->setText(QString::number(jValue[i],'f',3));
        if(axisLimit.size()==jValue.size())
        {
            double tmpEqualValue=100.0*(jValue[i]-axisLimit[i][0])/(axisLimit[i][1]-axisLimit[i][0]);
            if(tmpEqualValue>100)
            {
                tmpEqualValue=100;
            }
            else if(tmpEqualValue<0)
            {
                tmpEqualValue=0;
            }
            jProgressValueBar[i]->setValue(tmpEqualValue);
        }

    }

    return ;

}



void JogView::jogUseLimit(int isJointJogIn,int totalAxisIn)
{
    ui->pushButton_L1->setEnabled(true);
    ui->pushButton_L2->setEnabled(true);
    ui->pushButton_L3->setEnabled(true);
    ui->pushButton_L4->setEnabled(true);
    ui->pushButton_L5->setEnabled(true);
    ui->pushButton_L6->setEnabled(true);
//    ui->pushButton_L7->setEnabled(true);

    ui->pushButton_R1->setEnabled(true);
    ui->pushButton_R2->setEnabled(true);
    ui->pushButton_R3->setEnabled(true);
    ui->pushButton_R4->setEnabled(true);
    ui->pushButton_R5->setEnabled(true);
    ui->pushButton_R6->setEnabled(true);
//    ui->pushButton_R7->setEnabled(true);

    if(!isJointJogIn)//笛卡尔显示
    {
        if(totalAxisIn == 4)
        {
            ui->pushButton_L4->setEnabled(false);
            ui->pushButton_L5->setEnabled(false);

            ui->pushButton_R4->setEnabled(false);
            ui->pushButton_R5->setEnabled(false);
        }
        else if(totalAxisIn == 5)
        {
            ui->pushButton_L4->setEnabled(false);
            ui->pushButton_L5->setEnabled(false);
            ui->pushButton_L6->setEnabled(false);

            ui->pushButton_R4->setEnabled(false);
            ui->pushButton_R5->setEnabled(false);
            ui->pushButton_R6->setEnabled(false);
        }
    }
}



void JogView::on_pushButton_L1_pressed()
{
    emit pushButton_L1_pressedSignal();
}

void JogView::on_pushButton_L2_pressed()
{
    emit pushButton_L2_pressedSignal();
}

void JogView::on_pushButton_L3_pressed()
{
    emit pushButton_L3_pressedSignal();
}

void JogView::on_pushButton_L4_pressed()
{
    emit pushButton_L4_pressedSignal();
}

void JogView::on_pushButton_L5_pressed()
{
    emit pushButton_L5_pressedSignal();
}

void JogView::on_pushButton_L6_pressed()
{
    emit pushButton_L6_pressedSignal();
}

//void JogView::on_pushButton_L7_pressed()
//{
//    emit pushButton_L7_pressedSignal();
//}

void JogView::on_pushButton_L1_released()
{
    emit pushButton_L1_releasedSignal();
}

void JogView::on_pushButton_L2_released()
{
    emit pushButton_L2_releasedSignal();
}

void JogView::on_pushButton_L3_released()
{
    emit pushButton_L3_releasedSignal();
}

void JogView::on_pushButton_L4_released()
{
    emit pushButton_L4_releasedSignal();
}

void JogView::on_pushButton_L5_released()
{
    emit pushButton_L5_releasedSignal();
}

void JogView::on_pushButton_L6_released()
{
    emit pushButton_L6_releasedSignal();
}

//void JogView::on_pushButton_L7_released()
//{
//    emit pushButton_L7_releasedSignal();
//}

void JogView::on_pushButton_R1_pressed()
{
    emit pushButton_R1_pressedSignal();
}

void JogView::on_pushButton_R2_pressed()
{
    emit pushButton_R2_pressedSignal();
}

void JogView::on_pushButton_R3_pressed()
{
    emit pushButton_R3_pressedSignal();
}

void JogView::on_pushButton_R4_pressed()
{
    emit pushButton_R4_pressedSignal();
}

void JogView::on_pushButton_R5_pressed()
{
    emit pushButton_R5_pressedSignal();
}

void JogView::on_pushButton_R6_pressed()
{
    emit pushButton_R6_pressedSignal();
}

//void JogView::on_pushButton_R7_pressed()
//{
//    emit pushButton_R7_pressedSignal();
//}

void JogView::on_pushButton_R1_released()
{
    emit pushButton_R1_releasedSignal();
}

void JogView::on_pushButton_R2_released()
{
    emit pushButton_R2_releasedSignal();
}

void JogView::on_pushButton_R3_released()
{
    emit pushButton_R3_releasedSignal();
}

void JogView::on_pushButton_R4_released()
{
    emit pushButton_R4_releasedSignal();
}

void JogView::on_pushButton_R5_released()
{
    emit pushButton_R5_releasedSignal();
}

void JogView::on_pushButton_R6_released()
{
    emit pushButton_R6_releasedSignal();
}

//void JogView::on_pushButton_R7_released()
//{
//    emit pushButton_R7_releasedSignal();
//}

void JogView::on_checkBox_sevon_clicked(bool checked)
{


    if(false==checked && EM_MOTOR_STATUS_SERVO_ON==globalData->globalServoState)
    {
        QMessageBox message(QMessageBox::Question, "note", "即将断开电机使能?"
                            , QMessageBox::Yes | QMessageBox::No, NULL);
        if(message.exec() == QMessageBox::No)
        {
            return ;
        }
    }


    emit manualChangeSignal(checked);
}

void JogView::on_button_clrMsgAndServoOn_clicked()
{
    emit clearMsgAndServoOn_signal();
}

void JogView::on_checkBox_joyStick_clicked(bool checked)
{
    emit displayJoyStickView_signal(checked);
}

void JogView::on_checkBox_dragMode_clicked(bool checked)
{
    if(globalData->globalHandState> EM_CONTROL_MODE_T2)
    {
        QMessageBox::information(NULL, "提示", "非手动模式，禁止切换拖拽功能！", QMessageBox::Yes );
        return ;
    }
    if(checked)
    {
        QMessageBox message(QMessageBox::Question, "note", "即将开启拖拽模式?"
                            , QMessageBox::Yes | QMessageBox::No, NULL);
        if(message.exec() == QMessageBox::No)
        {
            return ;
        }
    }
    emit setDragControl_signal(checked);
}
